package com.yousu.dashijixm.controller;

import com.yousu.dashijixm.pojo.Article;
import com.yousu.dashijixm.pojo.PageBean;
import com.yousu.dashijixm.pojo.Result;
import com.yousu.dashijixm.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: YouSu
 * @Version: 1.0
 * @CreateTime: 2023-11-19  10:55
 * 文章管理控制层
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PostMapping //添加/发布文章信息
    public Result add(@RequestBody @Validated Article article){
        articleService.add(article);
        return Result.success();
    }
    @PutMapping //更新文章
    public Result update(@RequestBody @Validated(Article.Update.class) Article article){
        articleService.update(article);
        return Result.success();
    }
    @GetMapping("/detail") //获取文章详细信息
    public Result<Article> detail(Integer id){
        Article article = articleService.detail(id);
        return Result.success(article);
    }
    @DeleteMapping//删除文章
    public Result delete(Integer id){
        articleService.delete(id);
        return Result.success();
    }

    @GetMapping //获取所有文章
    public Result<PageBean<Article>> list(
            Integer pageNum, //当前页码
            Integer pageSize, //每页条数
            @RequestParam(required = false) Integer categoryId,//搜索框中的文章分类ID
            @RequestParam(required = false) String state////搜索框中的发布状态
    ){

        PageBean<Article> pa = articleService.list(pageNum,pageSize,categoryId,state);
        return Result.success(pa);
    }



}
